package com.yanqu.road.server.manager.config;

import com.yanqu.road.entity.servercenter.ChannelStatus;
import com.yanqu.road.logic.bussiness.servercenter.SystemSwitchBusiness;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.utils.ConfigHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CrossSystemSwitchMgr extends TempMgr {
    private static Map<Integer, ChannelStatus> channelStatusMap = new HashMap<>();
    private static Map<Long, List<Integer>> lockSystemIdListMap = new HashMap<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        Map<Integer, ChannelStatus> tempChannelMap = SystemSwitchBusiness.getChannelStatusMap(ConfigHelper.getInt("channelId"));
        Map<Long, List<Integer>> tempLockMap = SystemSwitchBusiness.getLockSystemIdList();
        channelStatusMap = tempChannelMap;
        lockSystemIdListMap = tempLockMap;
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static boolean isSystemOpen(int systemId) {
        ChannelStatus channelStatus = channelStatusMap.get(systemId);
        if (channelStatus == null) {
            return true;
        }
        //1是把系统打开，0是把系统关闭
        return channelStatus.getStatus() == 1;
    }

    public static boolean isServerLockSystem(long serverId, int systemId) {
        List<Integer> systemIdList = lockSystemIdListMap.get(serverId);
        if (systemIdList == null) {
            return false;
        }
        return systemIdList.contains(systemId);
    }
}
